package zhuy.scroll_touch;




import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UseScrollerTouchActivity extends Activity {

	@SuppressWarnings("unused")
	private static final String TAG = "scroller";

	private MyViewGroup viewGroup;

	private int[] drawableResources = new int[] { R.drawable.a1, R.drawable.a2,
			R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6 };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		viewGroup = new MyViewGroup(this);
		setContentView(viewGroup);
		

		GridView gridview =new GridView(this);
		gridview.setNumColumns(3);
		//GridView gridview =(GridView)LayoutInflater.from(this).inflate(R.layout.grid, null);
		gridview.setAdapter(new GridAdapter(this));
		viewGroup.addView(gridview);
		
		
		for (int drawableId : drawableResources) {
			ImageView imageView = new ImageView(this);
			imageView.setImageDrawable(getResources().getDrawable(drawableId));
			viewGroup.addView(imageView);
		}
		
		
		
		
		
		
		
	}
	
	class GridAdapter extends BaseAdapter
	{
		Context context;
		public GridAdapter(Context c)
		{
			context=c;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return drawableResources.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return drawableResources[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LinearLayout v=null;
			 if (convertView == null) {  // if it's not recycled, initialize some attributes
				 v=(LinearLayout)LayoutInflater.from(context).inflate(R.layout.grid_item, null);
				 //v=(LinearLayout)View.inflate(mContext, R.layout.grid_item, null);
		        } else {
		            v = (LinearLayout) convertView;
		        }
			 ImageView iv=(ImageView)v.findViewById(R.id.gridimg);
			 TextView tv=(TextView)v.findViewById(R.id.gridtit);
			 			
			 try {
				iv.setImageResource(drawableResources[position]);
				tv.setText("pos:"+position);
			}  catch (Exception e) {
				e.printStackTrace();
			}
			
			
		    return v;
		}
		
	}
}